//
//  PopoverAnimator.swift
//  CBWWeiBo
//
//  Created by 陈博文 on 16/1/9.
//  Copyright © 2016年 陈博文. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {

    // MARK:- 属性
    var presentedFrame = CGRectZero
    
}
extension PopoverAnimator : UIViewControllerTransitioningDelegate {
    /// 当弹出一个控制器时会调用该方法
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let presentController = PresentationController(presentedViewController: presented, presentingViewController: presenting)
        presentController.presentedFrame = presentedFrame
        
        return presentController
    }
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
//设置出现通知
       NSNotificationCenter.defaultCenter().postNotificationName(CBWPopoverPresentedViewNoification, object: nil)
        
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  //设置消失通知
        NSNotificationCenter.defaultCenter().postNotificationName(CBWPopoverDismissedViewNoification, object: nil)
        
        return self
    }
}

extension PopoverAnimator : UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.25
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // 1.拿到弹出的View,如果可以取到值表示弹出
        if let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey) {
            // 2.将弹出的View添加到容器的视图中
            transitionContext.containerView()?.addSubview(presentedView)
            
            // 3.给弹出的View一个动画
            presentedView.transform = CGAffineTransformMakeScale(1.0, 0)
            presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                presentedView.transform = CGAffineTransformIdentity
                }, completion: { (isFinished) -> Void in
                    transitionContext.completeTransition(true)
            })
        } else { // 如果presentedView没有取到值,表示是消失动画
            // 1.取出消失的View
            let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            
            // 2.给消失的View添加动画
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                dismissView.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                }, completion: { (_) -> Void in
                    dismissView.removeFromSuperview()
                    transitionContext.completeTransition(true)
            })
        }
    }
}
